package xmpp.androidpush.service;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import java.io.Serializable;

/**
 * 推送配置，配置服务器、端口、用户名、密码、资源等。
 */
public class PushConfig implements Serializable {
    private static final long serialVersionUID = 7968513392311662784L;
    public static final String RESOURCE = "pushservice";
    public static final String CONN_STARTUP = "start_up";
    public static final String SERVER = "server";
    public static final String PORT = "port";
    public static final String USER = "user";
    public static final String PASS = "pass";

    public static final String REQUIRE_SSL = "require_ssl";
    public static final String CONNECT_TIMEOUT = "connect_timeout";
    String server;
    int port;
    String user;
    String pass;
    String resource = "pushservice";
    boolean require_ssl;
    int connectTimeout;

    public static boolean isSatrtUp(Context ctx) {
        return PreferenceManager.getDefaultSharedPreferences(ctx).getBoolean(CONN_STARTUP, false);
    }

    static void setStartUp(Context ctx, boolean startUp) {
        PreferenceManager.getDefaultSharedPreferences(ctx).edit().putBoolean(CONN_STARTUP, startUp).commit();
    }

    static PushConfig getInstance(Context ctx) {
        SharedPreferences sf = PreferenceManager.getDefaultSharedPreferences(ctx);
        PushConfig config = new PushConfig();
        config.server = sf.getString(SERVER, "");
        config.port = sf.getInt(PORT, 5222);
        config.user = sf.getString(USER, "");
        config.pass = sf.getString(PASS, "");
        config.require_ssl = sf.getBoolean(REQUIRE_SSL, false);
        config.connectTimeout = sf.getInt(CONNECT_TIMEOUT, 15);
        config.resource = sf.getString(RESOURCE, "pushservice");
        return config;
    }

    public static void saveXmppConfig(Context ctx, PushConfig config) {
        SharedPreferences.Editor sfe = PreferenceManager.getDefaultSharedPreferences(ctx).edit();
        sfe.putString(SERVER, config.server);
        sfe.putInt(PORT, config.port);
        sfe.putString(USER, config.user);
        sfe.putString(PASS, config.pass);
        sfe.putBoolean(REQUIRE_SSL, config.require_ssl);
        sfe.putInt(CONNECT_TIMEOUT, config.connectTimeout);
        sfe.commit();
    }

    private PushConfig() {
    }

    public PushConfig(String server, int port, String user, String pass) {
        this.server = server;
        this.port = port;
        this.user = user;
        this.pass = pass;
        this.connectTimeout = 15;
        this.require_ssl = false;
    }

    public PushConfig(String server, int port, String user, String pass, String resource) {
        this.server = server;
        this.port = port;
        this.user = user;
        this.pass = pass;
        this.resource = resource;
        this.connectTimeout = 15;
        this.require_ssl = false;
    }

    /**
     * 设置是否启用SSL
     *
     * @param require_ssl
     */
    public void setRequireSSL(boolean require_ssl) {
        this.require_ssl = require_ssl;
    }

    /**
     * 设置连接超时
     *
     * @param connectTimeout
     */
    public void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }
}
